package com.lu.pojo.thread;

/**
 * @author luzhenfang
 * @date 2020 05 23
 * @description 龟兔赛跑
 */
public class Race implements Runnable {
    private String winner;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(Thread.currentThread().getName().equals("兔子")&&i%60==0){
                System.out.println(Thread.currentThread().getName()+"Sleeping....");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
            if(gameOver(i)){
                break;
            }

        }
    }
    private boolean gameOver(int steep){
        // 出现了胜利者
        if (winner!=null){
           return true;
        }else if(steep>=100){
           winner=Thread.currentThread().getName();
            System.out.println("winner is "+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Race race = new Race();
        new Thread(race,"乌龟").start();
        new Thread(race,"兔子").start();
    }
}
